﻿using System;
using System.Security.Permissions;
using System.Windows.Forms;

namespace ContinuousDisintegration.UserReactivationTool
{
    /// <summary>
    /// Summary description for MyControl.
    /// </summary>
    public partial class UserReactivationUI : UserControl
    {
        public UserReactivationUI()
        {
            InitializeComponent();
            SubscribeToEvents();
            LoadOrganizations();
        }

        private void LoadOrganizations()
        {
            cbOrganization.Items.Add("Client A");
            cbOrganization.Items.Add("Client B");
            cbOrganization.Items.Add("Client C");
        }

        private void SubscribeToEvents()
        {
            cbOrganization.SelectedIndexChanged += new System.EventHandler(cbOrganization_SelectedIndexChanged);
            cbUser.SelectedIndexChanged += new System.EventHandler(cbUser_SelectedIndexChanged);
            btnReactivateUser.Click += new EventHandler(btnReactivateUser_Click);
        }

        void btnReactivateUser_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.MessageBox.Show(
                String.Format("User {0} Reactivated.", cbUser.SelectedItem));
        }

        void cbUser_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            btnReactivateUser.Enabled = true;
        }

        private void LoadUsers(string selectedClient)
        {
            string clientSpecificRyanUser = "Ryan - {0}";
            string clientSpecificJoeUser = "Joe - {0}";
            string clientSpecificBillUser = "Bill - {0}";

            cbUser.Items.Clear();
            cbUser.Items.Add(String.Format(clientSpecificRyanUser, selectedClient));
            cbUser.Items.Add(String.Format(clientSpecificBillUser, selectedClient));
            cbUser.Items.Add(String.Format(clientSpecificJoeUser, selectedClient));
        }

        void cbOrganization_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            cbUser.Enabled = true;
            LoadUsers(cbOrganization.SelectedItem.ToString());
        }

        /// <summary> 
        /// Let this control process the mnemonics.
        /// </summary>
        [UIPermission(SecurityAction.LinkDemand, Window = UIPermissionWindow.AllWindows)]
        protected override bool ProcessDialogChar(char charCode)
        {
              // If we're the top-level form or control, we need to do the mnemonic handling
              if (charCode != ' ' && ProcessMnemonic(charCode))
              {
                    return true;
              }
              return base.ProcessDialogChar(charCode);
        }

        /// <summary>
        /// Enable the IME status handling for this control.
        /// </summary>
        protected override bool CanEnableIme
        {
            get
            {
                return true;
            }
        }
    }
}
